package cn.edu.scnu.springbootcinema.controller;

import cn.edu.scnu.springbootcinema.entity.Actor;
import cn.edu.scnu.springbootcinema.entity.Comments;
import cn.edu.scnu.springbootcinema.entity.Movie;
import cn.edu.scnu.springbootcinema.entity.User;
import cn.edu.scnu.springbootcinema.service.ActorService;
import cn.edu.scnu.springbootcinema.service.CommentsService;
import cn.edu.scnu.springbootcinema.service.MovieService;
import cn.edu.scnu.springbootcinema.service.UserFavoriteService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/movie")
public class MovieController {

    @Resource
    private MovieService movieService;

    @Resource
    private ActorService actorService;

    @Resource
    private CommentsService commentsService;

    @Resource
    private UserFavoriteService userFavoriteService;

    @GetMapping("/detail")
    public String getMovieDetail(Integer id, Model model) {
        System.out.println(id);
        Movie movie = movieService.findMovieById(id);
        List<Actor> actors = actorService.getActorsByMovieId(movie.getId());
        List<Comments> comments = commentsService.getCommentsByMovieId(movie.getId());
        List<Movie> relatedMovies = movieService.getRelatedMovies(id, movie.getType());

        model.addAttribute("movie", movie);
        model.addAttribute("actors", actors);
        model.addAttribute("comments", comments);
        model.addAttribute("relatedMovies", relatedMovies);
        return "movie-detail";
    }

    @GetMapping("/play")
    public String playMovie(Integer id, Model model) {
        Movie movie = movieService.findMovieById(id);
        if (movie == null) {
            model.addAttribute("error", "电影未找到");
            return "error";
        }
        model.addAttribute("movie", movie);
        return "movie-play";
    }

    @GetMapping("/getAllMovies")
    @ResponseBody
    public List<Movie> getAllMovies() {
        return movieService.getAllMovies();
    }

    @GetMapping("/getHotMovies")
    @ResponseBody
    public List<Movie> getHotMovies() {
        return movieService.getHotMovies();
    }

    @GetMapping("/getLatestMovies")
    @ResponseBody
    public List<Movie> getLatestMovies() {
        return movieService.getLatestMovies();
    }

    @GetMapping("/searchQuery")
    @ResponseBody
    public List<Movie> searchQuery(String query) {
        return movieService.searchByQuery(query);
    }

    @GetMapping("/favorite")
    @ResponseBody
    public List<Movie> favoriteMovie(HttpSession session) {
        User user = (User) session.getAttribute("user");
        int userId = user.getUserId();
        return userFavoriteService.getFavoriteMoviesByUserId(userId);
    }

}